package com.fjnu.config;

import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import javax.sql.DataSource;

/**
 * @desc:
 * @author: Ping
 * @date: 2025/10/17 8:53
 */

@Configuration
@PropertySource("classpath:db.properties")
public class DataSourceConfig {
    @Value("${mysql.driver}")
    private String driver;
    @Value("${mysql.url}")
    private String url;
    @Value("${mysql.username}")
    private String username ;
    @Value("${mysql.password}")
    private String password ;
    @Bean
//    public DataSource dataSource() {
//        DriverManagerDataSource dataSource = new DriverManagerDataSource();
//        dataSource.setDriverClassName(driver);
//        dataSource.setUrl(url);
//        dataSource.setUsername(username);
//        dataSource.setPassword(password);
//        return dataSource;
//    }

    public DataSource dataSource() {
        /**
         * 创建一个 HikariCP  Connection Pool dataSource
         */
        HikariDataSource dataSource = new HikariDataSource() ;
        dataSource.setDriverClassName(driver);
        dataSource.setJdbcUrl(url);
        dataSource.setUsername(username);
            dataSource.setPassword(password);
        return dataSource;

    }

    /**
     * DataSourceTransactionManager
     * @param dataSource
     * @return
     *
     */
    @Bean
    public DataSourceTransactionManager transactionManager(@Autowired DataSource dataSource) {

        return new DataSourceTransactionManager(dataSource);
    }





    @Bean
    public JdbcTemplate jdbcTemplate(@Autowired  DataSource dataSource) {

        return new JdbcTemplate(dataSource);
    }


}
